﻿using SPSDeploy.Extensions.Modules.Update;
using System.Xml.Linq;
using System.Xml.XPath;

namespace SPSDeploy.Extensions.Controllers
{
    /// <summary>
    /// Update operation controlle
    /// </summary>
    public class UpdateController : BaseController
    {
 
        // Private members
        private readonly string fileName;

        /// <summary>
        /// Initialization
        /// </summary>
        /// <param name="fileName"></param>
        public UpdateController(string fileName)
        {
            this.fileName = fileName;
        }

        /// <summary>
        /// Execute operation
        /// </summary>
        public override void Execute()
        {
            OnProgressMessage("Update started.");
            XElement root = XElement.Load(fileName);
            new FarmModule(this).Execute(root.XPathSelectElement("/farm"));
            new ServiceApplicationModule(this).Execute(root.XPathSelectElement("/farm/serviceApplications"));
            foreach (XElement element in root.XPathSelectElements("/farm/serviceApplications/serviceApplication[@type='Search Service Application']"))
                new SearchModule(this).Execute(element);
            new ServiceApplicationProxyModule(this).Execute(root.XPathSelectElement("/farm/serviceApplicationProxies"));
            foreach (XElement element in root.XPathSelectElements("/farm/serviceApplicationProxies/serviceApplicationProxy[@type='Search Service Application Proxy']"))
                new SearchProxyModule(this).Execute(element);
            OnProgressMessage("Update completed.");
        }

   }
}